package com.smartcar.service;

import com.smartcar.encode.CarEncode;
import com.smartcar.enums.CarDirection;
import com.smartcar.tcp.TCPClientManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 小车API服务
 * 对应原HarmonyOS的CarApi
 */
@Slf4j
@Service
public class CarApiService {

    private final TCPClientManager tcpClientManager;

    public CarApiService(TCPClientManager tcpClientManager) {
        this.tcpClientManager = tcpClientManager;
    }

    /**
     * 按钮控制小车
     */
    public void carBtnCtrl(CarDirection direction) {
        String message = CarEncode.buttonCarEncode(direction);
        send(message);
    }

    /**
     * 发送消息
     */
    public void send(String message) {
        tcpClientManager.sendMessage(message)
                .thenAccept(success -> {
                    if (success) {
                        log.debug("消息发送成功: {}", message);
                    } else {
                        log.error("消息发送失败: {}", message);
                    }
                });
    }

    /**
     * 控制小车速度
     */
    public void ctrlCar(int speedX, int speedY) {
        String message = CarEncode.ctrlCarEncode(speedX, speedY);
        send(message);
    }

    /**
     * 四轮单独控制
     */
    public void upSpeedCar(int l1, int l2, int r1, int r2) {
        String message = CarEncode.upSpeedCarEncode(l1, l2, r1, r2);
        send(message);
    }

    /**
     * 开始循迹
     */
    public void startTracking() {
        String message = CarEncode.trackingOpenEncode();
        send(message);
    }

    /**
     * 停止循迹
     */
    public void stopTracking() {
        String message = CarEncode.trackingCloseEncode();
        send(message);
    }

    /**
     * 拍摄图片
     */
    public void takePhoto() {
        String message = CarEncode.takePhotosEncode();
        send(message);
    }

    /**
     * 开始录像
     */
    public void startRecording() {
        String message = CarEncode.startRecordingEncode();
        send(message);
    }

    /**
     * 停止录像
     */
    public void stopRecording() {
        String message = CarEncode.closeRecordingEncode();
        send(message);
    }
} 